#include "cat.h"

#define ID_LEN 20
#define NAME_LEN 20
#define DATA_LEN 30

//主菜单
void menu(){
    system("clear");
    printf("\t********************************************************\n");
    printf("\t*\t\t      猫猫管理系统                     *\n");
    printf("\t********************************************************\n");
    printf("\t*                                                      *\n");
    printf("\t*1.添加猫猫信息     2.显示猫猫信息      3.查找猫猫信息 *\n");
    printf("\t*                                                      *\n");
    printf("\t*4.修改猫猫信息                         5.删除猫猫信息 *\n");
    printf("\t*                                                      *\n");
    printf("\t*6.退出系统                                            *\n");
    printf("\t*                                                      *\n");
    printf("\t********************************************************\n");
}

//控制台
void control(LNode* pList){
    int choose;
    while(1){
        menu();
        printf("\n\n请输入你要办理的业务: ");
        scanf("%d",&choose);
        switch(choose){
            case 1:
                do{
                    system("clear");
                    inputaddCat(pList);
                    char sel[5];
                    printf("\n\n\t\t是否继续添加(y or n):");
                    scanf("%s",sel);
                    if(strcmp(sel,"y")==0) continue;
                    else break;
                }while(1);
                break;
            case 2:
                system("clear");
                printf("\t欢迎来到查看界面,请按要求完成操作!\n");
                showCat(pList);
                printf("\n\n\n退出请输入0:");
                int sel2;
                scanf("%d",&sel2);
                if(sel2==0) break;
                break;
            case 3:
                do{
                    system("clear");
                    printf("\t欢迎来到查找界面,请按要求完成操作!\n");
                    seekCat(pList);
                    printf("\n\n\t\t\t是否继续查找(y or n): ");
                    char sel3[5];
                    scanf("%s",sel3);
                    if(strcmp(sel3,"y")==0) continue;
                    else break;
                }while(1);
                break;
            case 4:
                system("clear");
                printf("\t欢迎来到修改界面,请按要求完成操作!\n");
                changeCat(pList);
                break;
            case 5:
                do{
                    system("clear");
                    delCat(pList);
                    char sel5[5];
                    printf("\t\t\n\n是否继续删除(y or n): ");
                    scanf("%s",sel5);
                    if(strcmp(sel5,"y")==0) continue;
                    else break;
                }while(1);
                break;
            case 6:
                exit(-1);
        }
    }
}

//添加猫猫信息
void inputaddCat(LNode* pList){
    printf("\t欢迎来到添加界面,请按要求完成操作!\n\n\n");
    LNode* q = (LNode*)malloc(sizeof(LNode));
    q->pNext = NULL;
    LNode* p;
    p = pList;
    char ID[ID_LEN];
    printf("请输入猫猫编号:");
    scanf("%s",ID);
    char name[NAME_LEN];
    printf("\n请输入猫猫姓名:");
    scanf("%s",name);
    while(p->pNext != NULL){
        if(strcmp(p->data.ID,ID) == 0 || strcmp(p->data.name,name) == 0){
            printf("\n\n该猫猫的信息撞名了,您是不是记错了呢？\n");
            break;
        }
        p = p->pNext;
    }
    if(p->pNext == NULL){
        strcpy(q->data.ID,ID);
        strcpy(q->data.name,name);
        printf(" \ng 表示公猫, m 表示母猫\n");
        printf("请输入猫猫性别:");
        getchar();
        scanf("%c",&q->data.sex);
        printf("\n请输入猫猫体重(kg):");
        scanf("%lf",&q->data.weight);
        printf("\n请输入猫猫出生日期:");
        scanf("%s",q->data.date);
        p->pNext = q;
        p = q;
        printf("\n\n添加成功!\n");
    }
    saveCat(pList);
}

//查看已有猫猫信息
void showCat(LNode* pList){
    travel(pList);
}

//查找某个猫猫信息
void seekCat(LNode* pList){
    char name2[NAME_LEN];
    printf("\n\n请输入猫猫姓名:\n");
    scanf("%s",name2);
    LNode* pfd = findname(pList,name2);
    if(NULL == pfd){
        printf("\t\t还未录入信息!!!\n");
    }else{
        printf("猫猫编号: %s\n",pfd->data.ID);
        printf("猫猫姓名：%s\n",pfd->data.name);
        printf("猫猫性别：%c\n",pfd->data.sex);
        printf("猫猫重量：%lf\n",pfd->data.weight);
        printf("猫猫出生日期：%s\n",pfd->data.date);
    }
}
LNode* findname(LNode* pList,char name1[20]){
    LNode* pname = pList;
    while(pname){
        if(strcmp(name1,pname->data.name)==0){
            printf("\n\n找到了\n");
            return pname;
        }
        pname = pname->pNext;
    }
    return NULL;
}

//修改某个猫猫信息,通过名字来修改信息
void changeCat(LNode* pList){
    char name4[NAME_LEN];
    printf("\n\n请输入猫猫姓名:\n");
    scanf("%s",name4);
    LNode* pch = findname(pList,name4);
    changeInput(pch);
    saveCat(pList);
}
void changeInput(LNode* p){
    if(NULL != p){
        printf("\t********************************************************\n");
        printf("\t********************************************************\n");
        printf("\t*1.编号               2.姓名              3.性别       *\n");
        printf("\t*4.重量                                   5.出生日期   *\n");
        printf("\t********************************************************\n");
        printf("\t********************************************************\n");
        int sel4;
        printf("\n请输入要修改信息的序号: ");
        scanf("%d",&sel4);
        switch(sel4){
            case 1: 
                printf("\n请输入修改后的编号: ");
                scanf("%s",p->data.ID);
                break;
            case 2:
                printf("\n请输入修改后的姓名: ");
                scanf("%s",p->data.name);
                break;
            case 3:
                printf("\n请输入修改后的性别: ");
                getchar();
                scanf("%c",&(p->data.sex));
                break;
            case 4:
                printf("\n请输入修改后的重量: ");
                scanf("%lf",&(p->data.weight));
                break;
            case 5:
                printf("\n请输入修改后的出生日期: ");
                scanf("%s",p->data.date);
                break;
        } 
    }
}

//删除某个猫猫信息
void delCat(LNode* pList){
    printf("\t欢迎来到删除界面,请按要求完成操作!\n\n\n");
    LNode* p = (LNode*)malloc(sizeof(LNode));
    LNode* q = (LNode*)malloc(sizeof(LNode));
    p = pList;
    q = p->pNext;
    char name5[NAME_LEN];
    if(NULL == p){
        printf("此时没有猫猫信息!");
        return;
    }else{
        printf("请输入你要删除的猫猫的名字: ");
        scanf("%s",name5);
        while(q!=NULL){
            if(strcmp(q->data.name,name5)==0){
                p->pNext = q->pNext;
                free(q);
                printf("\t\t恭喜您!删除信息成功！！！\n");
            }
            p = q;
            q = q->pNext;
        }
    saveCat(pList);
    }
}